import {
  getSingerUrl
} from 'api/singer';
import {
  getLyric
} from 'api/song';

import {
  Base64
} from 'js-base64';

export const Singer = class Singer {
  constructor({
    id,
    name,
    mid
  }) {
    this.id = id
    this.name = name
    this.singer_mid = mid
    this.imgUrl = `https://y.gtimg.cn/music/photo_new/T001R300x300M000${mid}.jpg?max_age=2592000`
  }
}


export class music {
  constructor({
    id,
    mid,
    name,
    songmid,
    duration
  }) {
    this.id = id
    this.name = name
    this.mid = mid
    this.songmid = songmid
    this.duration = duration
    this.img = `https://y.gtimg.cn/music/photo_new/T002R300x300M000${mid}.jpg?max_age=2592000`
    getSingerUrl(songmid).then(res => {
      this.url = res
    })
  }
  getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }
    return new Promise((resolve, reject) => {
      getLyric(this.songmid).then(res => {
        if (res.code === 0) {
          this.lyric = Base64.decode(res.lyric);
          resolve(this.lyric)
        } else {
          reject('no song.js')
        }
      })
    })
  }
}

export function musicInfo(item) {
  return new music({
    id: item.albumid,
    mid: item.albummid,
    name: item.songname,
    songmid: item.songmid,
    duration: item.interval
  })
}
